<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Messages App</title>
    <style>
        [v-cloak] {display: none;}
        body > div {width: 500px; margin: 0 auto;}
        textarea {width: 100%;} 
        ul {padding: 0 15px;}
    </style>
</head>
<body>
    <div id="app" v-cloak>
        <message-list :items="messages" @delete="deleteMessage"></message-list> 

        <form v-on:submit.prevent="addMessage">
            <textarea v-focus v-model="newMessage" placeholder="Leave a message">
            </textarea>
            <div>
                <button :disabled="addDisabled" type="submit">Add</button>
            </div>
        </form>
    </div>
    <script src="../jslib/vue_2.7.14.js"></script>
    <script type="module">
        import MessageList from './components/MessageList.js';
        import './directives/focus.directive.js';
        import './filters/datetime.filter.js';
        import lifecyleLogger from './mixins/lifecycle-logger.mixin.js';
        import LifecycleLogger from './plugins/lifecycle-logger.plugin.js'

        Vue.use(LifecycleLogger, {beforeMount: false}) 

        window.vm = new Vue({
            name: 'MessagesApp', 
            el: "#app",
            mixins: [lifecyleLogger],
            data() {
                return {
                    messages: [],
                    newMessage: ""
                };
            },
            methods: {
                addMessage(event) {
                    if(!this.newMessage) {
                        return;
                    }
                    let now = new Date();
                    this.messages.push({id: now.toString(), text: this.newMessage, createdAt: now});
                    this.newMessage = '';
                },
                deleteMessage(message) {
                    this.messages.splice(this.messages.indexOf(message), 1);
                }
            },
            computed: {
                addDisabled() {
                    return this.messages.length > 10 || this.newMessage.length > 50;
                }
            },
            components: {
                MessageList
            }
        })
    </script>
    
</body>
</html>